package com.example.apricots.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.apricots.bean.MenuListBean;
import com.example.apricots.R;

public class MyMenuAdapter extends BaseAdapter {

    private Context context;
    private MenuListBean[] mDatas;
    private MyMenuAdapter.MenuItemListener menuItemListener;

    public MyMenuAdapter(Context context, MenuListBean[] listBean,MyMenuAdapter.MenuItemListener listener) {
        this.context = context;
        this.mDatas = listBean;
        this.menuItemListener=listener;
    }

    @Override
    public int getCount() {
        return mDatas.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_menu, null);
            holder.iv = view.findViewById(R.id.iv_image);
            holder.tv = view.findViewById(R.id.tv_text);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv.setText(mDatas[position].getTitile());
        holder.iv.setImageResource(mDatas[position].getImg());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context, mDatas[position].getTitile() + "被点击了！", Toast.LENGTH_SHORT).show();
                menuItemListener.onMenuItemClickListener(context,position);
            }
        });

        return view;
    }

    static class ViewHolder {
        //图片和描述
        TextView tv;
        ImageView iv;
        //头像

    }

    public interface MenuItemListener {
        void onMenuItemClickListener(Context context,int position);
    }

}
